﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using com.huawei.esdk.tp.professional.DataType;

namespace eSDK_TP_Demo_CS_CSharp.FunctionPage
{
    /// <summary>
    /// 预约会议
    /// </summary>
    public partial class ScheduleConf_UI : UserControl
    {
        AuthorizeService loginService = new AuthorizeService();
        //public ConferenceInfoEx confInfo;
        ConferenceService service = new ConferenceService();
        public List<SiteInfoEx> siteList;
        List<SiteInfoEx> allSites = null;

        public ScheduleConf_UI()
        {
            InitializeComponent();
            GetSiteList();
            Initialize();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            this.BeginTimeText.Text = DateTime.Now.AddMinutes(30).ToString();

            Dictionary<int, string> mediaEncryptTypeDic = new Dictionary<int, string>()
            {
                {0,"AutoEncrypt"},
                {1,"ForceEncrypt"},
                {2,"None"}
            };

            this.mediaEncryptTypeCombx.ItemsSource = mediaEncryptTypeDic;
            this.mediaEncryptTypeCombx.SelectedValuePath = "Key";
            this.mediaEncryptTypeCombx.DisplayMemberPath = "Value";
            this.mediaEncryptTypeCombx.SelectedValue = 0;

            Dictionary<int, string> cpResouceDic = new Dictionary<int, string>()
            {
                {1,"1"},
                {2,"2"},
                {3,"3"},
                {4,"4"},
                {5,"5"},
                {6,"6"},
                {7,"7"},
                {8,"8"}
            };

            this.cpResouceCombx.ItemsSource = cpResouceDic;
            this.cpResouceCombx.SelectedValuePath = "Key";
            this.cpResouceCombx.DisplayMemberPath = "Value";
            this.cpResouceCombx.SelectedValue = 1;

            Dictionary<int, string> auxVideoFormatDic = new Dictionary<int, string>()
            {
                {0,"4CIF"},
                {1,"720P30"},
                {2,"1080P30"},
                {3,"480P"},
                {4,"720P60"},
                {5,"1080P60"}
            };

            this.auxVideoFormatCombx.ItemsSource = auxVideoFormatDic;
            this.auxVideoFormatCombx.SelectedValuePath = "Key";
            this.auxVideoFormatCombx.DisplayMemberPath = "Value";
            this.auxVideoFormatCombx.SelectedValue = 0;

            Dictionary<int, string> auxVideoProtocolDic = new Dictionary<int, string>()
            {
                {0,"H.261"},
                {1,"H.263"},
                {2,"H.264"}
            };

            this.auxVideoProtocolCombx.ItemsSource = auxVideoProtocolDic;
            this.auxVideoProtocolCombx.SelectedValuePath = "Key";
            this.auxVideoProtocolCombx.DisplayMemberPath = "Value";
            this.auxVideoProtocolCombx.SelectedValue = 0;

            Dictionary<int, string> RateDic = new Dictionary<int, string>()
            {
                {0,"64K"},
                {1,"1920K"},
                {2,"8192K"}
            };

            this.rateComBx.ItemsSource = RateDic;
            this.rateComBx.SelectedValuePath = "Key";
            this.rateComBx.DisplayMemberPath = "Value";
            this.rateComBx.SelectedValue = 1;
        }

        private void ScheduleConf_UI_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.SiteInfoList.Items.Count != 0)
            {
                this.SiteInfoList.Items.Refresh();
            }
        }

        private void SiteListBut_Click(object sender, RoutedEventArgs e)
        {
            GetSiteList();
        }

        /// <summary>
        /// 获取所有会场的信息列表
        /// </summary>
        private void GetSiteList()
        {
            if (allSites == null)
            {
                loginService.Login();
                allSites = service.QuerySites();
                if (allSites != null)
                {
                    this.SiteInfoList.ItemsSource = allSites;
                }
                loginService.Logout();
            }
            else
                this.SiteInfoList.ItemsSource = allSites;
        }

        private void AddSite_Click(object sender, RoutedEventArgs e)
        {
            siteList = new List<SiteInfoEx>();
            int count = this.SiteInfoList.SelectedItems.Count;
            if (count >= 1)
            {
                foreach (SiteInfoEx site in this.SiteInfoList.SelectedItems)
                {
                    siteList.Add(site);
                }
                this.SiteInfoList.ItemsSource = siteList;
            }
        }

        private void DelSite_Click(object sender, RoutedEventArgs e)
        {
            foreach (SiteInfoEx se in this.SiteInfoList.SelectedItems)
            {
                siteList.Remove(se);
            }
            this.SiteInfoList.Items.Refresh();
        }
        
        private void CreateConf_Click(object sender, RoutedEventArgs e)
        {
            loginService.Login();
            //会议开始时间
            DateTime beginTime;
            try
            {
                //会议名称
                string name = this.ConfName.Text;
                beginTime = DateTime.Parse(this.BeginTimeText.Text);

                //会议时长
                string duration = this.DurationText.Text;
                //接入码
                string accessCode = this.AccessCodeText.Text;
                //密码
                string password = this.PasswordText.Text;
                //加密方式
                string mediaEncryptType = this.mediaEncryptTypeCombx.SelectedValue.ToString();
                //多画面模式
                string cpResource = this.cpResouceCombx.SelectedValue.ToString();
                //辅流视频格式
                string auxVideoFormat = this.auxVideoFormatCombx.SelectedValue.ToString();
                //视频协议
                string auxVideoProtocol = this.auxVideoProtocolCombx.SelectedValue.ToString();
                //速率
                string rate = this.rateComBx.Text;
                //添加到会议的会场
                SiteInfoEx[] siteArray = null;
                if (siteList != null)
                {
                    siteArray = new SiteInfoEx[siteList.Count];
                    for (int i = 0; i < siteList.Count; i++)
                    {
                        siteArray[i] = siteList[i];
                    }
                }
                ResultCodeClass result = service.AddScheduleConf(name, beginTime, duration, accessCode, password,
                    mediaEncryptType, cpResource, auxVideoFormat, auxVideoProtocol, rate, siteArray);

                if (result.Result == "0")
                {
                    this.confIDText.Text = result.ConfInfo.confId;
                    this.OperationSucceedText.Visibility = Visibility.Visible;
                    this.OperationDefeatText.Visibility = Visibility.Collapsed;
                    this.ConfIDGrid.Visibility = Visibility.Visible;
                }
                else
                {
                    this.OperationSucceedText.Visibility = Visibility.Collapsed;
                    this.OperationDefeatText.Visibility = Visibility.Visible;
                    this.ConfIDGrid.Visibility = Visibility.Collapsed;
                }
                this.ResultCodeText.Text = result.Result;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                loginService.Logout();
            }
        }

        private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.Line1.X2 = this.GridWidth.ActualWidth;
            this.Line2.X2 = this.GridWidth.ActualWidth;
            this.Line3.X2 = this.GridWidth.ActualWidth;
        }
    }
}
